package com.mofang.ticketing.common.security.bo;

import com.mofang.ticketing.common.core.enums.base.SysEnableEnum;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 *  用户详情
 *
 * @author LGC
 */
@Data
public class UserDetailBO implements UserDetails {
    private UserInfoBO userInfoBO;
    private List<RoleInfoBO> roleList;
    private Set<String> roles;
    private Set<String> perms;

    public UserDetailBO(UserInfoBO userInfoBO, List<RoleInfoBO> roleList, Set<String> roles, Set<String> perms) {
        this.userInfoBO = userInfoBO;
        this.roleList = roleList;
        this.roles = roles;
        this.perms = perms;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authorities = new ArrayList<>();
        for (RoleInfoBO role : this.roleList) {
            authorities.add(new SimpleGrantedAuthority(role.getRoleCode()));
        }

        return authorities;
    }

    @Override
    public String getPassword() {
        return userInfoBO.getPassword();
    }

    @Override
    public String getUsername() {
        return userInfoBO.getAccount();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return userInfoBO.getIsEnable().equals(SysEnableEnum.ENABLE.getKey());
    }
}
